﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using RapChieuPhim.BUS;
using System.Collections;

namespace RapChieuPhim.DAO
{
    class PhongChieuDAO
    {
        public PhongChieuDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaPhong", id);
            DataTable dt = cn.SqlExcuteQuery("LayPhongChieuTheoID", param);
            PhongChieuDTO PhongChieu = new PhongChieuDTO();
            if(dt.Rows.Count != 0)
            {
                PhongChieu.MaPhong = int.Parse(dt.Rows[0]["MaPhong"].ToString());
                PhongChieu.SoHangGhe = int.Parse(dt.Rows[0]["SoHangGhe"].ToString());
                PhongChieu.SoDayGhe = int.Parse(dt.Rows[0]["SoDayGhe"].ToString());
                PhongChieu.MoTa = dt.Rows[0]["MoTa"].ToString();
                PhongChieu.DkKiThuat = Boolean.Parse(dt.Rows[0]["DKKiThuat"].ToString());
                PhongChieu.TinhTrang = Boolean.Parse(dt.Rows[0]["TinhTrang"].ToString());
                PhongChieu.Rap = RapBUS.GetByID(int.Parse(dt.Rows[0]["MaRap"].ToString()));
            }
            return PhongChieu;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllPhongChieu");
            if (dt.Rows.Count != 0)
            {
                foreach(DataRow dr in dt.Rows)
                {
                    PhongChieuDTO PhongChieu = new PhongChieuDTO();
                    PhongChieu.MaPhong = int.Parse(dr["MaPhong"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(dr["SoHangGhe"].ToString());
                    PhongChieu.SoDayGhe = int.Parse(dr["SoDayGhe"].ToString());
                    PhongChieu.MoTa = dr["MoTa"].ToString();
                    PhongChieu.DkKiThuat = Boolean.Parse(dr["DKKiThuat"].ToString());
                    PhongChieu.TinhTrang = Boolean.Parse(dr["TinhTrang"].ToString());
                    PhongChieu.Rap = RapBUS.GetByID(int.Parse(dr["MaRap"].ToString()));
                    array.Add(PhongChieu);
                }
            }
            return array;
        }

        public void Insert(PhongChieuDTO PhongChieu)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@MaPhong", PhongChieu.MaPhong);
            param[1] = new SqlParameter("@SoHangGhe", PhongChieu.SoHangGhe);
            param[2] = new SqlParameter("@SoDayGhe", PhongChieu.SoDayGhe);
            param[3] = new SqlParameter("@MoTa", PhongChieu.MoTa);
            param[4] = new SqlParameter("@DKKiThuat", PhongChieu.DkKiThuat);
            param[5] = new SqlParameter("@TinhTrang", PhongChieu.TinhTrang);
            param[6] = new SqlParameter("@MaRap", PhongChieu.Rap);
            cn.SqlExcuteNonQuery("InsertPhongChieu", param);
        }

        public void Update(PhongChieuDTO PhongChieu)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@MaPhong", PhongChieu.MaPhong);
            param[1] = new SqlParameter("@SoHangGhe", PhongChieu.SoHangGhe);
            param[2] = new SqlParameter("@SoDayGhe", PhongChieu.SoDayGhe);
            param[3] = new SqlParameter("@MoTa", PhongChieu.MoTa);
            param[4] = new SqlParameter("@DKKiThuat", PhongChieu.DkKiThuat);
            param[5] = new SqlParameter("@TinhTrang", PhongChieu.TinhTrang);
            param[6] = new SqlParameter("@MaRap", PhongChieu.Rap);
            cn.SqlExcuteNonQuery("UpdatePhongChieu", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("MaPhong", id);
            cn.SqlExcuteNonQuery("DeletePhongChieu", param);
        }
    }
}
